package Q3;

import java.util.Scanner;

public class Main {

    public static int v;
    public static int n;
    public static int[] arr;
    public static int ret = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        v = scanner.nextInt();
        n = scanner.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        dfs(0, 0, 0);

        System.out.println(ret);
    }

    public static void dfs(int index, int sumWeight, int preWeight) {

        if (ret == 0) {
            return;
        }

        if (sumWeight >= v) {
            if (sumWeight == v) {
                ret = 0;
                return;
            }
            ret = Math.min(ret, sumWeight - preWeight);

            return;
        }

        for (int i = index; i < n; i++) {
            //选
            sumWeight += arr[i];
            dfs(i + 1, sumWeight, arr[i]);
            //回溯
            sumWeight -= arr[i];

            //不选
            dfs(i + 1, sumWeight, preWeight);
        }
    }
}
